<!DOCTYPE html>
<html lang="cn">

<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="锁的原理 锁是怎么实现的？怎么做到独占的？实际上得从原子操作说起。
原子操作 原子操作不是说一定要只能一个动作，而是说把它包装成一条指令或者一个函数的时候，它可以保证不会被侵入。在单核CPU上，一条指令肯定是原子的，但不见得一条指令就只做了一个操作，可能是两个操作，比如CAS(Compare&amp;Set或Compare&amp;Swap，X86下对应的是CMPXCHG汇编指令)是比较并交换操作数，这在单核上没有问题，但在多核上如何保证不被打断呢？这就需要硬件来支持了。
我们在intel IA-64手册中，可以翻到这样一条指令前缀: 按文档描述，把这条前缀加到某些指令前时，它可以实现一个原子操作。在x86体系中，CPU里专门有一条引线叫HLOCK pin，当遇到有LOCK前缀的指令就拉低该引线电位，从而锁住了总线。那么在同一总线上的其他Core就无法通过总线访问内存了。所以原子操作的本质仍然是锁，只是这个锁是硬件层面的。也不存在软件层面上真正意义的无锁操作，底层仍然是锁。
我们在Go的源码中也能看到大量这样的代码:
// src/runtime/internal/atomic/asm_amd64.s TEXT runtime∕internal∕atomic·Cas(SB),NOSPLIT,$0-17 MOVQ	ptr&#43;0(FP), BX MOVL	old&#43;8(FP), AX MOVL	new&#43;12(FP), CX LOCK CMPXCHGL	CX, 0(BX) SETEQ	ret&#43;16(FP) RET 随着现在CPU核数逐步增多，这种LOCK前缀锁总线的方式带来的性能问题就凸显了出来，所以现在的体系遇到LOCK前缀时，不是去锁整个总线，而是先检查一下要锁的内容是不是在cache中，如果在的话只锁那一行cacheline，只有同时访问这一个cacheline的其他core才会被锁住，就像数据库的表锁和行锁。这样一来，性能的影响没那么大了，但是原子操作还是会带来性能影响的，只是硬件层面的东西我们作为程序员改变不了什么也就很少去提。
那么原子操作和我们平时所说的互斥锁有什么区别呢？我们谈原子操作是只针对一个操作，而锁是往往是要包含一段逻辑、一个block的。
无锁结构 也就是Lock Free，它的做法用伪码来表达是这样的:
for { old = atomic.Load() if (atomic.CAS(old, new)){ break } } // CAS 伪码 func compareAndSwap(addr int, old int, new int) bool{ if (*addr != old){ return False } *addr = new return True } 当old通过原子操作读出以后，如果有其他CPU操作修改了这个值，那么atomic.CAS就会发现old变了则修改失败重新循环，反之修改成功退出循环，这就是无锁结构。运气好的话一次就执行完了，运气不好一直被打断的话循环多少次是未知的。它的实际使用场景比如说是一个无锁结构的链表队列，当需要向队列尾部插入元素时，插入不成功则说明有其他线程刚刚插入过，队列尾部的地址已经变了，则需重试。"><meta property="og:title" content="锁的原理" />
<meta property="og:description" content="" />
<meta property="og:type" content="website" />
<meta property="og:url" content="/docs/go/lock/" />

<title>锁的原理 | Home</title>
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/book.min.63eb88daa545365405ecdbb21033286a325c60a36cfa6d22d21e7c3bc9286941.css" integrity="sha256-Y&#43;uI2qVFNlQF7NuyEDMoajJcYKNs&#43;m0i0h58O8koaUE=">
<script defer src="/cn.search.min.8d2462c3b745732864d843e8e2e7782a2ec838ca90fb94676baa461536bdae11.js" integrity="sha256-jSRiw7dFcyhk2EPo4ud4Ki7IOMqQ&#43;5Rna6pGFTa9rhE="></script>
<link rel="alternate" type="application/rss+xml" href="/docs/go/lock/index.xml" title="Home" />
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->

  
</head>

<body>
  <input type="checkbox" class="hidden" id="menu-control" />
  <main class="container flex">
    <aside class="book-menu">
      
  <nav>
<h2 class="book-brand">
  <a href="/"><img src="/logo.png" alt="Logo" /><span>Home</span>
  </a>
</h2>


<div class="book-search">
  <input type="text" id="book-search-input" placeholder="搜索" aria-label="搜索" maxlength="64" data-hotkeys="s/" />
  <div class="book-search-spinner spinner hidden"></div>
  <ul id="book-search-results"></ul>
</div>











  <ul>
<li><strong>计算机基础</strong>
<ul>
<li><a href="/docs/sicp/hardware/">硬件</a></li>
<li><a href="/docs/sicp/software/">软件</a></li>
<li><a href="/docs/sicp/program/">程序</a></li>
<li><a href="/docs/sicp/asm/">汇编</a></li>
</ul>
</li>
<li><strong>GO语言</strong>
<ul>
<li><a href="/docs/go/map/">字典</a></li>
<li><a href="/docs/go/closure/">闭包</a></li>
<li><a href="/docs/go/defer/">延迟调用</a></li>
<li><a href="/docs/go/goroutine/">并发调度</a></li>
<li><a href="/docs/go/alloc/">内存分配</a></li>
<li><a href="/docs/go/gc/">垃圾回收</a></li>
<li><a href="/docs/go/lock/"class=active>锁</a></li>
</ul>
</li>
<li><strong>Python语言</strong>
<ul>
<li><a href="/docs/python/memory/">内存管理</a></li>
<li><a href="/docs/python/interpreter/">解释器</a></li>
<li><a href="/docs/python/tools/">技巧工具</a></li>
</ul>
</li>
<li><strong>Mysql</strong>
<ul>
<li><a href="/docs/mysql/query/">查询</a></li>
<li><a href="/docs/mysql/theory/">原理</a></li>
</ul>
</li>
<li><strong>其他</strong>
<ul>
<li><a href="/docs/other/git/">git</a></li>
<li><a href="/docs/other/docker/">docker</a></li>
<li><a href="/docs/other/raft/">raft</a></li>
<li><a href="/docs/other/shell/">shell</a></li>
<li><a href="/docs/other/oop/">面向对象</a></li>
<li><a href="/docs/other/protocol/">网络协议</a></li>
<li><a href="/docs/other/tools/">系统工具</a></li>
</ul>
</li>
</ul>










</nav>




  <script>(function(){var menu=document.querySelector("aside.book-menu nav");addEventListener("beforeunload",function(event){localStorage.setItem("menu.scrollTop",menu.scrollTop);});menu.scrollTop=localStorage.getItem("menu.scrollTop");})();</script>


 
    </aside>

    <div class="book-page">
      <header class="book-header">
        
  <div class="flex align-center justify-between">
  <label for="menu-control">
    <img src="/svg/menu.svg" class="book-icon" alt="Menu" />
  </label>

  <strong>锁的原理</strong>

  <label for="toc-control">
    <img src="/svg/toc.svg" class="book-icon" alt="Table of Contents" />
  </label>
</div>


  
    <input type="checkbox" class="hidden" id="toc-control" />
    <aside class="hidden clearfix">
      
  <nav id="TableOfContents">
  <ul>
    <li><a href="#原子操作">原子操作</a></li>
    <li><a href="#无锁结构">无锁结构</a></li>
    <li><a href="#缓存一致性">缓存一致性</a></li>
    <li><a href="#go语言中的锁">Go语言中的锁</a>
      <ul>
        <li><a href="#futex">Futex</a></li>
        <li><a href="#sema">Sema</a></li>
        <li><a href="#mutex">Mutex</a></li>
      </ul>
    </li>
    <li><a href="#总结">总结</a></li>
  </ul>
</nav>


    </aside>
  
 
      </header>

      
      
  <article class="markdown"><h1 id="锁的原理">锁的原理</h1>
<p>锁是怎么实现的？怎么做到独占的？实际上得从原子操作说起。</p>
<h2 id="原子操作">原子操作</h2>
<p>原子操作不是说一定要只能一个动作，而是说把它包装成一条指令或者一个函数的时候，它可以保证不会被侵入。在单核CPU上，一条指令肯定是原子的，但不见得一条指令就只做了一个操作，可能是两个操作，比如<code>CAS</code>(Compare&amp;Set或Compare&amp;Swap，X86下对应的是CMPXCHG汇编指令)是比较并交换操作数，这在单核上没有问题，但在多核上如何保证不被打断呢？这就需要硬件来支持了。</p>
<p>我们在<a href="https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf">intel IA-64手册</a>中，可以翻到这样一条指令前缀:
<img src="images/lock.jpg" alt="" /></p>
<p>按文档描述，把这条前缀加到某些指令前时，它可以实现一个原子操作。在x86体系中，CPU里专门有一条引线叫HLOCK pin，当遇到有LOCK前缀的指令就拉低该引线电位，从而锁住了总线。那么在同一总线上的其他Core就无法通过总线访问内存了。所以原子操作的本质仍然是锁，只是这个锁是硬件层面的。也不存在软件层面上真正意义的无锁操作，底层仍然是锁。</p>
<p>我们在Go的源码中也能看到大量这样的代码:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-asm" data-lang="asm"><span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">src</span><span style="color:#960050;background-color:#1e0010">/</span><span style="color:#66d9ef">runtime</span><span style="color:#960050;background-color:#1e0010">/</span><span style="color:#66d9ef">internal</span><span style="color:#960050;background-color:#1e0010">/</span><span style="color:#66d9ef">atomic</span><span style="color:#960050;background-color:#1e0010">/</span><span style="color:#66d9ef">asm_amd64.s</span>
<span style="color:#a6e22e">TEXT</span> <span style="color:#66d9ef">runtime</span><span style="color:#960050;background-color:#1e0010">∕</span><span style="color:#66d9ef">internal</span><span style="color:#960050;background-color:#1e0010">∕</span><span style="color:#66d9ef">atomic</span><span style="color:#960050;background-color:#1e0010">·</span><span style="color:#66d9ef">Cas</span>(<span style="color:#66d9ef">SB</span>),<span style="color:#66d9ef">NOSPLIT</span>,<span style="color:#66d9ef">$0-17</span>
	<span style="color:#a6e22e">MOVQ</span>	<span style="color:#66d9ef">ptr</span><span style="color:#960050;background-color:#1e0010">+</span><span style="color:#ae81ff">0</span>(<span style="color:#66d9ef">FP</span>), <span style="color:#66d9ef">BX</span>
	<span style="color:#a6e22e">MOVL</span>	<span style="color:#66d9ef">old</span><span style="color:#960050;background-color:#1e0010">+</span><span style="color:#ae81ff">8</span>(<span style="color:#66d9ef">FP</span>), <span style="color:#66d9ef">AX</span>
	<span style="color:#a6e22e">MOVL</span>	<span style="color:#66d9ef">new</span><span style="color:#960050;background-color:#1e0010">+</span><span style="color:#ae81ff">12</span>(<span style="color:#66d9ef">FP</span>), <span style="color:#66d9ef">CX</span>
	<span style="color:#a6e22e">LOCK</span>
	<span style="color:#a6e22e">CMPXCHGL</span>	<span style="color:#66d9ef">CX</span>, <span style="color:#ae81ff">0</span>(<span style="color:#66d9ef">BX</span>)
	<span style="color:#a6e22e">SETEQ</span>	<span style="color:#66d9ef">ret</span><span style="color:#960050;background-color:#1e0010">+</span><span style="color:#ae81ff">16</span>(<span style="color:#66d9ef">FP</span>)
	<span style="color:#a6e22e">RET</span></code></pre></div>
<p>随着现在CPU核数逐步增多，这种LOCK前缀锁总线的方式带来的性能问题就凸显了出来，所以现在的体系遇到LOCK前缀时，不是去锁整个总线，而是先检查一下要锁的内容是不是在cache中，如果在的话只锁那一行cacheline，只有同时访问这一个cacheline的其他core才会被锁住，就像数据库的表锁和行锁。这样一来，性能的影响没那么大了，但是原子操作还是会带来性能影响的，只是硬件层面的东西我们作为程序员改变不了什么也就很少去提。</p>
<p>那么原子操作和我们平时所说的互斥锁有什么区别呢？我们谈原子操作是只针对一个操作，而锁是往往是要包含一段逻辑、一个block的。</p>
<h2 id="无锁结构">无锁结构</h2>
<p>也就是Lock Free，它的做法用伪码来表达是这样的:</p>
<pre><code>for {
    old = atomic.Load()

    if (atomic.CAS(old, new)){
        break
    }
}

// CAS 伪码
func compareAndSwap(addr int, old int, new int) bool{
    if (*addr != old){
        return False
    }
    *addr = new
    return True
}
</code></pre><p>当old通过原子操作读出以后，如果有其他CPU操作修改了这个值，那么<code>atomic.CAS</code>就会发现old变了则修改失败重新循环，反之修改成功退出循环，这就是无锁结构。运气好的话一次就执行完了，运气不好一直被打断的话循环多少次是未知的。它的实际使用场景比如说是一个无锁结构的链表队列，当需要向队列尾部插入元素时，插入不成功则说明有其他线程刚刚插入过，队列尾部的地址已经变了，则需重试。</p>
<p>这种结构也有个问题，就是ABA的问题。试想一下<code>atomic.Load</code>读出来是A，然后被人修改为了B再修改回A，<code>atomic.CAS</code>就发现不了接着执行自己的逻辑。这样粗看好像也没有什么问题，但CAS只是检查的一个地址，实际情况往往会有其他的逻辑，有人做了一个比喻:你拿着一个装满钱的手提箱在飞机场，此时过来了一个火辣性感的美女，然后她很暖昧地挑逗着你，并趁你不注意的时候，把用一个一模一样的手提箱和你那装满钱的箱子调了个包，然后就离开了，你看到你的手提箱还在那，于是就提着手提箱去赶飞机去了。</p>
<p>怎么解决ABA的问题呢？可以采用数据库乐观锁的办法，每次进行修改的时候添加一个时间戳或者流水号，上例就是<code>atomic.Load</code>读出来以后就是A20191222，修改为B就变为了B20191223，再进行一次修改就是A20191224了。</p>
<h2 id="缓存一致性">缓存一致性</h2>
<p>CPU是不会和内存直接交互的，它和内存之间的读写都是通过L1、L2、L3缓存来做中转。那么如果内存上有个X=1，CPU1和CPU2都把这个X读到自己的缓存上，CPU1这时候对X做了修改让X=3，CPU2又如何知道呢？这就是缓存一致性的问题，需要确保的是任一时刻，缓存的内容是相同的。</p>
<p>缓存是以行为单位的，也就是cacheline，每行中除了缓存的数据外还包括内存地址和状态。而状态又分为四种，即M、E、S、I。M即modify，表示当前CPU修改过数据，需要以此为准回写到主存中去；E即exclusive，独占，当前的CPU独占了这个数据，和主存中是一致的，其他CPU没有的；S即shared，共享，各个CPU都有这行数据，且是和主存一致的；I即invalid，表示失效的，CPU发现是这个状态就意味着需要从主存里重新去拿一下。</p>
<p>当CPU2要去写某一行数据的时候，它就会向总线发出一个请求: CPU2要独占某一段地址。CPU1、CPU3、CPU4一直都在嗅探总线去接收总线的通知，然后去检查自己的缓存中有没有这行数据，如果CPU3有且这行状态是共享的，那就把CPU3的这行变为失效状态；但如果CPU1那行已经是独占的，这个CPU2就得等，等到CPU1修改完数据回写到主存当中会解除CPU1的独占状态，CPU2重新从主存刷新自己的缓存再把它修改为独占状态。修改了数据就需要改为modify，因为需要一批数据一同刷入主存。</p>
<p>这种通过四种状态保证缓存一致性的方式和我们日常见到的锁的原理差不多，数据库修改某行的时候也是加行锁，其他人要修改该行就得排队，要修改其他行就不受影响。</p>
<h2 id="go语言中的锁">Go语言中的锁</h2>
<h3 id="futex">Futex</h3>
<p>锁的本质是通过某种操作实现休眠、唤醒等操作，那它就一定要由操作系统来实现，由OS实现才能避免对于休眠的单位不去分配时间片。那么锁就需要进行系统调用，它的效率就会非常低？Linux在很早期的版本就提供了一种锁<a href="https://zh.wikipedia.org/wiki/Futex">Futex</a>，虽然是由操作系统提供的，但它是在用户空间实现的，是一种特殊的系统调用，大部分情况下不需要进入内核空间，避免了通常的系统调用所需的用户态内核态之间的切换。Futex其实就是一块内存空间，通常是一个整型变量，大家通过原子操作对它做修改，要么就修改成功执行逻辑，要么就修改失败等待它。</p>
<p>Linux手册中提供了这种锁的<a href="http://man7.org/linux/man-pages/man2/futex.2.html">调用方式</a>，包括锁定、等待、唤醒等参数。Go中调用其API:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// src/runtime/os_linux.go
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">futex</span>(<span style="color:#a6e22e">addr</span> <span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>, <span style="color:#a6e22e">op</span> <span style="color:#66d9ef">int32</span>, <span style="color:#a6e22e">val</span> <span style="color:#66d9ef">uint32</span>, <span style="color:#a6e22e">ts</span>, <span style="color:#a6e22e">addr2</span> <span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>, <span style="color:#a6e22e">val3</span> <span style="color:#66d9ef">uint32</span>) <span style="color:#66d9ef">int32</span>

<span style="color:#75715e">// Linux futex.
</span><span style="color:#75715e">//
</span><span style="color:#75715e">//	futexsleep(uint32 *addr, uint32 val)
</span><span style="color:#75715e">//	futexwakeup(uint32 *addr)
</span><span style="color:#75715e">//
</span><span style="color:#75715e">// Futexsleep atomically checks if *addr == val and if so, sleeps on addr.
</span><span style="color:#75715e">// Futexwakeup wakes up threads sleeping on addr.
</span><span style="color:#75715e">// Futexsleep is allowed to wake up spuriously.
</span><span style="color:#75715e"></span>

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">futexsleep</span>(<span style="color:#a6e22e">addr</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">uint32</span>, <span style="color:#a6e22e">val</span> <span style="color:#66d9ef">uint32</span>, <span style="color:#a6e22e">ns</span> <span style="color:#66d9ef">int64</span>) {
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">ns</span> &lt; <span style="color:#ae81ff">0</span> {
		<span style="color:#a6e22e">futex</span>(<span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(<span style="color:#a6e22e">addr</span>), <span style="color:#a6e22e">_FUTEX_WAIT_PRIVATE</span>, <span style="color:#a6e22e">val</span>, <span style="color:#66d9ef">nil</span>, <span style="color:#66d9ef">nil</span>, <span style="color:#ae81ff">0</span>)
		<span style="color:#66d9ef">return</span>
	}

	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">ts</span> <span style="color:#a6e22e">timespec</span>
	<span style="color:#a6e22e">ts</span>.<span style="color:#a6e22e">setNsec</span>(<span style="color:#a6e22e">ns</span>)
	<span style="color:#a6e22e">futex</span>(<span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(<span style="color:#a6e22e">addr</span>), <span style="color:#a6e22e">_FUTEX_WAIT_PRIVATE</span>, <span style="color:#a6e22e">val</span>, <span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">ts</span>), <span style="color:#66d9ef">nil</span>, <span style="color:#ae81ff">0</span>)
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">futexwakeup</span>(<span style="color:#a6e22e">addr</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">uint32</span>, <span style="color:#a6e22e">cnt</span> <span style="color:#66d9ef">uint32</span>) {
	<span style="color:#a6e22e">ret</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">futex</span>(<span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(<span style="color:#a6e22e">addr</span>), <span style="color:#a6e22e">_FUTEX_WAKE_PRIVATE</span>, <span style="color:#a6e22e">cnt</span>, <span style="color:#66d9ef">nil</span>, <span style="color:#66d9ef">nil</span>, <span style="color:#ae81ff">0</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">ret</span> <span style="color:#f92672">&gt;=</span> <span style="color:#ae81ff">0</span> {
		<span style="color:#66d9ef">return</span>
	}

	<span style="color:#a6e22e">systemstack</span>(<span style="color:#66d9ef">func</span>() {
		print(<span style="color:#e6db74">&#34;futexwakeup addr=&#34;</span>, <span style="color:#a6e22e">addr</span>, <span style="color:#e6db74">&#34; returned &#34;</span>, <span style="color:#a6e22e">ret</span>, <span style="color:#e6db74">&#34;\n&#34;</span>)
	})

	<span style="color:#f92672">*</span>(<span style="color:#f92672">*</span><span style="color:#66d9ef">int32</span>)(<span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(uintptr(<span style="color:#ae81ff">0x1006</span>))) = <span style="color:#ae81ff">0x1006</span>
}</code></pre></div>
<p>有了API，并不能直接用，还要对其进行包装:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// src/runtime/lock_futex.go
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">lock</span>(<span style="color:#a6e22e">l</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">mutex</span>) {
	<span style="color:#a6e22e">gp</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">getg</span>()

	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">locks</span> &lt; <span style="color:#ae81ff">0</span> {
		<span style="color:#a6e22e">throw</span>(<span style="color:#e6db74">&#34;runtime·lock: lock count&#34;</span>)
	}
	<span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">locks</span><span style="color:#f92672">++</span>

	<span style="color:#75715e">// 尝试原子操作修改l.key的值为锁定状态
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xchg</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#a6e22e">mutex_locked</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutex_unlocked</span> {
		<span style="color:#66d9ef">return</span>
	}
    <span style="color:#75715e">// 尝试失败则进入自旋状态
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">wait</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">v</span>
	<span style="color:#a6e22e">spin</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">ncpu</span> &gt; <span style="color:#ae81ff">1</span> {
		<span style="color:#a6e22e">spin</span> = <span style="color:#a6e22e">active_spin</span>
	}
	<span style="color:#66d9ef">for</span> {
		<span style="color:#75715e">// 每自旋一次尝试拿一次锁
</span><span style="color:#75715e"></span>		<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">spin</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
			<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutex_unlocked</span> {
				<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Cas</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#a6e22e">mutex_unlocked</span>, <span style="color:#a6e22e">wait</span>) {
					<span style="color:#66d9ef">return</span>
				}
			}
			<span style="color:#a6e22e">procyield</span>(<span style="color:#a6e22e">active_spin_cnt</span>)
		}

		<span style="color:#75715e">// 在rescheduling状态进行尝试拿锁
</span><span style="color:#75715e"></span>		<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">passive_spin</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
			<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutex_unlocked</span> {
				<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Cas</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#a6e22e">mutex_unlocked</span>, <span style="color:#a6e22e">wait</span>) {
					<span style="color:#66d9ef">return</span>
				}
			}
			<span style="color:#a6e22e">osyield</span>()
		}

		<span style="color:#75715e">// 尝试进入睡眠状态
</span><span style="color:#75715e"></span>		<span style="color:#a6e22e">v</span> = <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xchg</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#a6e22e">mutex_sleeping</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutex_unlocked</span> {
			<span style="color:#66d9ef">return</span>
		}
        <span style="color:#75715e">// 进入futex睡眠状态
</span><span style="color:#75715e"></span>		<span style="color:#a6e22e">wait</span> = <span style="color:#a6e22e">mutex_sleeping</span>
		<span style="color:#a6e22e">futexsleep</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#a6e22e">mutex_sleeping</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>)
	}
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">unlock</span>(<span style="color:#a6e22e">l</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">mutex</span>) {
	<span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xchg</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#a6e22e">mutex_unlocked</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutex_unlocked</span> {
		<span style="color:#a6e22e">throw</span>(<span style="color:#e6db74">&#34;unlock of unlocked lock&#34;</span>)
	}
    <span style="color:#75715e">// futex唤醒操作
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutex_sleeping</span> {
		<span style="color:#a6e22e">futexwakeup</span>(<span style="color:#a6e22e">key32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">l</span>.<span style="color:#a6e22e">key</span>), <span style="color:#ae81ff">1</span>)
	}

	<span style="color:#a6e22e">gp</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">getg</span>()
	<span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">locks</span><span style="color:#f92672">--</span>
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">locks</span> &lt; <span style="color:#ae81ff">0</span> {
		<span style="color:#a6e22e">throw</span>(<span style="color:#e6db74">&#34;runtime·unlock: lock count&#34;</span>)
	}
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">locks</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">preempt</span> {
		<span style="color:#a6e22e">gp</span>.<span style="color:#a6e22e">stackguard0</span> = <span style="color:#a6e22e">stackPreempt</span>
	}
}</code></pre></div>
<p>其一开始尝试拿锁属于投机，先以最乐观的情况考虑，如果没人竞争就能直接拿到锁，这种概率并不低，我们自己做性能设计时也可以参考它先设计乐观的情况。尝试失败则进入自旋状态，自旋状态打个比方，就是你在火车上上厕所，发现厕所有人，你在外面焦急的转圈等待；它是次一级的理想状态，因为厕所的人出来你马上就能进去，若是回到座位上可能被人插队；<code>procyield(active_spin_cnt)</code>背后会调用一个专门的CPU指令<a href="http://c9x.me/x86/html/file_module_x86_id_232.html">PAUSE</a>，它可以降低自旋状态时CPU的功耗并进入一个短暂的等待。自旋时没拿到锁则进入另一个状态，相当于回到座位上但是盯着厕所的门，这个状态下执行的<code>osyield()</code>是操作系统提供的等待，这种等待的时长就比CPU指令长很多，同时涉及到状态切换开销也会大很多。这种积极的尝试如果仍然失败，则进入睡眠状态，等待厕所里面的人出来唤醒它，唤醒后重新进入这个循环。</p>
<p>尽管做了这样的包装，这种锁仍然属于较低层次的，一般不会给用户直接用。</p>
<h3 id="sema">Sema</h3>
<p>信号量，可以控制同时执行的数量，如果数量是1就相当于互斥锁，如果都在执行了再进来人就得排队。</p>
<p>首先，它的结构是这样的:
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// src/runtime/sema.go
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">semaRoot</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">lock</span>  <span style="color:#a6e22e">mutex</span>
	<span style="color:#a6e22e">treap</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">sudog</span> <span style="color:#75715e">// root of balanced tree of unique waiters.
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">nwait</span> <span style="color:#66d9ef">uint32</span> <span style="color:#75715e">// Number of waiters. Read w/o the lock.
</span><span style="color:#75715e"></span>}</code></pre></div></p>
<p><code>lock</code> 就是之前设计的那种锁，在这之上提供了一个<code>treap</code>这样的平衡树结构，它是等待人(G对象)的列表，还有<code>nwait</code>计数器存储等待人的数量。</p>
<p>它的核心获取锁的逻辑:
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">semacquire1</span>(<span style="color:#a6e22e">addr</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">uint32</span>, <span style="color:#a6e22e">lifo</span> <span style="color:#66d9ef">bool</span>, <span style="color:#a6e22e">profile</span> <span style="color:#a6e22e">semaProfileFlags</span>, <span style="color:#a6e22e">skipframes</span> <span style="color:#66d9ef">int</span>) {
    <span style="color:#75715e">//简单的情况，即直接成功获取锁
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">cansemacquire</span>(<span style="color:#a6e22e">addr</span>) {
		<span style="color:#66d9ef">return</span>
	}
	<span style="color:#f92672">...</span>
    <span style="color:#75715e">// 复杂的情况:
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//  1.增加等待者计数；
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//  2.再尝试一次获取锁，成功则返回；
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//  3.将自己enqueue waiter；
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//  4.睡眠
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">for</span> {
		<span style="color:#a6e22e">lock</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">lock</span>)
		<span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xadd</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">nwait</span>, <span style="color:#ae81ff">1</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">cansemacquire</span>(<span style="color:#a6e22e">addr</span>) {
			<span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xadd</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">nwait</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>)
			<span style="color:#a6e22e">unlock</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">lock</span>)
			<span style="color:#66d9ef">break</span>
		}
		<span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">queue</span>(<span style="color:#a6e22e">addr</span>, <span style="color:#a6e22e">s</span>, <span style="color:#a6e22e">lifo</span>)
		<span style="color:#a6e22e">goparkunlock</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">lock</span>, <span style="color:#a6e22e">waitReasonSemacquire</span>, <span style="color:#a6e22e">traceEvGoBlockSync</span>, <span style="color:#ae81ff">4</span><span style="color:#f92672">+</span><span style="color:#a6e22e">skipframes</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">ticket</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">||</span> <span style="color:#a6e22e">cansemacquire</span>(<span style="color:#a6e22e">addr</span>) {
			<span style="color:#66d9ef">break</span>
		}
	}
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">releasetime</span> &gt; <span style="color:#ae81ff">0</span> {
		<span style="color:#a6e22e">blockevent</span>(<span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">releasetime</span><span style="color:#f92672">-</span><span style="color:#a6e22e">t0</span>, <span style="color:#ae81ff">3</span><span style="color:#f92672">+</span><span style="color:#a6e22e">skipframes</span>)
	}
	<span style="color:#a6e22e">releaseSudog</span>(<span style="color:#a6e22e">s</span>)
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">cansemacquire</span>(<span style="color:#a6e22e">addr</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">uint32</span>) <span style="color:#66d9ef">bool</span> {
	<span style="color:#66d9ef">for</span> {
		<span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Load</span>(<span style="color:#a6e22e">addr</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Cas</span>(<span style="color:#a6e22e">addr</span>, <span style="color:#a6e22e">v</span>, <span style="color:#a6e22e">v</span><span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>) {
			<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
		}
	}
}</code></pre></div>
首先尝试去锁定，并给等待者的数量加上1，然后通过<code>cansemacquire</code>检查一下能不能获得这把锁，能获得则减1退出循环。不能获得则加入到队列中，<code>goparkunlock</code>休眠。信号量归根结底是用原子操作来维护某个地址上的信号量加减，用一个锁来维护一个等待者计数器，这里的<code>&amp;root.lock</code>是为了保护对计数器的操作和入队的操作。</p>
<p>接着我们看看释放:
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">semrelease1</span>(<span style="color:#a6e22e">addr</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">uint32</span>, <span style="color:#a6e22e">handoff</span> <span style="color:#66d9ef">bool</span>, <span style="color:#a6e22e">skipframes</span> <span style="color:#66d9ef">int</span>) {
	<span style="color:#a6e22e">root</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">semroot</span>(<span style="color:#a6e22e">addr</span>)
	<span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xadd</span>(<span style="color:#a6e22e">addr</span>, <span style="color:#ae81ff">1</span>)

	<span style="color:#75715e">// 简单的情况：没有等待者
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Load</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">nwait</span>) <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
		<span style="color:#66d9ef">return</span>
	}

	<span style="color:#75715e">// 复杂的情况：找到一个等待者并唤醒它
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">lock</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">lock</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Load</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">nwait</span>) <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
		<span style="color:#a6e22e">unlock</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">lock</span>)
		<span style="color:#66d9ef">return</span>
	}
	<span style="color:#a6e22e">s</span>, <span style="color:#a6e22e">t0</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">dequeue</span>(<span style="color:#a6e22e">addr</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">s</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">Xadd</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">nwait</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>)
	}
	<span style="color:#a6e22e">unlock</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">lock</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">s</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> { <span style="color:#75715e">// May be slow or even yield, so unlock first
</span><span style="color:#75715e"></span>		<span style="color:#a6e22e">acquiretime</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">acquiretime</span>
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">acquiretime</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#a6e22e">mutexevent</span>(<span style="color:#a6e22e">t0</span><span style="color:#f92672">-</span><span style="color:#a6e22e">acquiretime</span>, <span style="color:#ae81ff">3</span><span style="color:#f92672">+</span><span style="color:#a6e22e">skipframes</span>)
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">ticket</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#a6e22e">throw</span>(<span style="color:#e6db74">&#34;corrupted semaphore ticket&#34;</span>)
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">handoff</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">cansemacquire</span>(<span style="color:#a6e22e">addr</span>) {
			<span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">ticket</span> = <span style="color:#ae81ff">1</span>
		}
		<span style="color:#a6e22e">readyWithTime</span>(<span style="color:#a6e22e">s</span>, <span style="color:#ae81ff">5</span><span style="color:#f92672">+</span><span style="color:#a6e22e">skipframes</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">s</span>.<span style="color:#a6e22e">ticket</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">1</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">getg</span>().<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">locks</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#a6e22e">goyield</span>()
		}
	}
}</code></pre></div></p>
<p>首先，释放必然使地址上的信号量加1。其次，去查看是否有人等，没人等则直接退出；如果有人等，则从队列中dequeue一个等待者、计数器减一，等待者可能是按地址排序的，但这属于其内部实现，我们没法确定弹出的是谁。最后如果传入的<code>handoff</code>为true表示要进行权限转移，会给这个弹出的等待者发一张票，之前<code>semacquire1</code>的for循环中<code>goparkunlock</code>休眠后被唤醒的第一件事就是检查有没有拿到票，拿到了则可以短路出去，没必要再走一次for循环和别人竞争抢锁<code>&amp;root.lock</code>。</p>
<p>信号量结合了它的特征，在一些短路的设计上值得我们学习。它仍然是runtime层面的，还不是交给用户去使用的。</p>
<h3 id="mutex">Mutex</h3>
<p>给用户使用的锁在标准库中，我们来看看它的实现:
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// src/sync/mutex.go 
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Mutex</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">state</span> <span style="color:#66d9ef">int32</span>
	<span style="color:#a6e22e">sema</span>  <span style="color:#66d9ef">uint32</span>
}
<span style="color:#66d9ef">const</span> (
	<span style="color:#a6e22e">mutexLocked</span> = <span style="color:#ae81ff">1</span> <span style="color:#f92672">&lt;&lt;</span> <span style="color:#66d9ef">iota</span> <span style="color:#75715e">// mutex is locked
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">mutexWoken</span>
	<span style="color:#a6e22e">mutexStarving</span>
	<span style="color:#a6e22e">mutexWaiterShift</span> = <span style="color:#66d9ef">iota</span>
)</code></pre></div></p>
<p>Mutex结构包括一个状态和一个计数，这个状态分为四种，即锁定、唤醒(这里可以理解为自旋)、饥饿、等待者转移。怎样会出现饥饿状态呢？比如A、B竞争一把锁的时候，A拿到了，B经过不断尝试最终进入了睡眠状态，A释放锁的时候又有C去竞争锁，C此时可能处于自旋状态，同理C后面可能还有D、E，B就一直拿不到锁处于饥饿状态。这种状态在并发编程中并不少见。</p>
<p>接着我们看看其锁定的过程:
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">m</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Mutex</span>) <span style="color:#a6e22e">Lock</span>() {
	<span style="color:#75715e">// 进入快速路径，乐观状态，没有竞争
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">CompareAndSwapInt32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>, <span style="color:#ae81ff">0</span>, <span style="color:#a6e22e">mutexLocked</span>) {
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">race</span>.<span style="color:#a6e22e">Enabled</span> {
			<span style="color:#a6e22e">race</span>.<span style="color:#a6e22e">Acquire</span>(<span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(<span style="color:#a6e22e">m</span>))
		}
		<span style="color:#66d9ef">return</span>
	}
	<span style="color:#75715e">// 进入慢速方式
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">lockSlow</span>()
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">m</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Mutex</span>) <span style="color:#a6e22e">lockSlow</span>() {
	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">waitStartTime</span> <span style="color:#66d9ef">int64</span>
	<span style="color:#a6e22e">starving</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">false</span>
	<span style="color:#a6e22e">awoke</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">false</span>
	<span style="color:#a6e22e">iter</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>
	<span style="color:#a6e22e">old</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>
	<span style="color:#66d9ef">for</span> { <span style="color:#75715e">// 依然通过循环处理
</span><span style="color:#75715e"></span>        <span style="color:#75715e">// 判断没有人处于饥饿状态，且还有可自旋次数，则进入自旋状态去尝试拿这把锁
</span><span style="color:#75715e"></span>		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span>(<span style="color:#a6e22e">mutexLocked</span>|<span style="color:#a6e22e">mutexStarving</span>) <span style="color:#f92672">==</span> <span style="color:#a6e22e">mutexLocked</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">runtime_canSpin</span>(<span style="color:#a6e22e">iter</span>) {
			<span style="color:#66d9ef">if</span> !<span style="color:#a6e22e">awoke</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">mutexWoken</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&gt;&gt;</span><span style="color:#a6e22e">mutexWaiterShift</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">&amp;&amp;</span>
				<span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">CompareAndSwapInt32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>, <span style="color:#a6e22e">old</span>, <span style="color:#a6e22e">old</span>|<span style="color:#a6e22e">mutexWoken</span>) {
				<span style="color:#a6e22e">awoke</span> = <span style="color:#66d9ef">true</span>
			}
			<span style="color:#a6e22e">runtime_doSpin</span>()
			<span style="color:#a6e22e">iter</span><span style="color:#f92672">++</span>
			<span style="color:#a6e22e">old</span> = <span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>
            <span style="color:#75715e">// 拿到锁则是mutexLocked状态，接着向下走；没拿到重新看可自旋次数。
</span><span style="color:#75715e"></span>			<span style="color:#66d9ef">continue</span>
		}
        <span style="color:#75715e">// 从这开始就有两种可能，要么拿到锁了，要么自旋次数用完了
</span><span style="color:#75715e"></span>		<span style="color:#a6e22e">new</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">old</span>
        <span style="color:#75715e">// 接着主要做一些状态变更和处理
</span><span style="color:#75715e"></span>		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">mutexStarving</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#a6e22e">new</span> <span style="color:#f92672">|=</span> <span style="color:#a6e22e">mutexLocked</span>
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span>(<span style="color:#a6e22e">mutexLocked</span>|<span style="color:#a6e22e">mutexStarving</span>) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#a6e22e">new</span> <span style="color:#f92672">+=</span> <span style="color:#ae81ff">1</span> <span style="color:#f92672">&lt;&lt;</span> <span style="color:#a6e22e">mutexWaiterShift</span>
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">starving</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">mutexLocked</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
			<span style="color:#a6e22e">new</span> <span style="color:#f92672">|=</span> <span style="color:#a6e22e">mutexStarving</span>
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">awoke</span> {
			<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">new</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">mutexWoken</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
				<span style="color:#a6e22e">throw</span>(<span style="color:#e6db74">&#34;sync: inconsistent mutex state&#34;</span>)
			}
			<span style="color:#a6e22e">new</span> <span style="color:#f92672">&amp;^=</span> <span style="color:#a6e22e">mutexWoken</span>
		}
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">CompareAndSwapInt32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>, <span style="color:#a6e22e">old</span>, <span style="color:#a6e22e">new</span>) {
            <span style="color:#75715e">// 没人上锁且没有饥饿状态的，则可以拿到锁跳出整个循环
</span><span style="color:#75715e"></span>			<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span>(<span style="color:#a6e22e">mutexLocked</span>|<span style="color:#a6e22e">mutexStarving</span>) <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
				<span style="color:#66d9ef">break</span> <span style="color:#75715e">// locked the mutex with CAS
</span><span style="color:#75715e"></span>			}
			<span style="color:#75715e">// 如果我之前已经在等待了，则排在队列的最前面
</span><span style="color:#75715e"></span>			<span style="color:#a6e22e">queueLifo</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">waitStartTime</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span>
			<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">waitStartTime</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
				<span style="color:#a6e22e">waitStartTime</span> = <span style="color:#a6e22e">runtime_nanotime</span>()
			}
			<span style="color:#a6e22e">runtime_SemacquireMutex</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">sema</span>, <span style="color:#a6e22e">queueLifo</span>, <span style="color:#ae81ff">1</span>)
            <span style="color:#75715e">// 判断时间是不是超出了饥饿状态的时间阈值
</span><span style="color:#75715e"></span>			<span style="color:#a6e22e">starving</span> = <span style="color:#a6e22e">starving</span> <span style="color:#f92672">||</span> <span style="color:#a6e22e">runtime_nanotime</span>()<span style="color:#f92672">-</span><span style="color:#a6e22e">waitStartTime</span> &gt; <span style="color:#a6e22e">starvationThresholdNs</span>
			<span style="color:#a6e22e">old</span> = <span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>
            <span style="color:#75715e">// 如果我自己就是饥饿的，那么我拿到锁，做相关状态修改，退出循环
</span><span style="color:#75715e"></span>			<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">mutexStarving</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
				<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&amp;</span>(<span style="color:#a6e22e">mutexLocked</span>|<span style="color:#a6e22e">mutexWoken</span>) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">||</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&gt;&gt;</span><span style="color:#a6e22e">mutexWaiterShift</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> {
					<span style="color:#a6e22e">throw</span>(<span style="color:#e6db74">&#34;sync: inconsistent mutex state&#34;</span>)
				}
				<span style="color:#a6e22e">delta</span> <span style="color:#f92672">:=</span> int32(<span style="color:#a6e22e">mutexLocked</span> <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">&lt;&lt;</span><span style="color:#a6e22e">mutexWaiterShift</span>)
				<span style="color:#66d9ef">if</span> !<span style="color:#a6e22e">starving</span> <span style="color:#f92672">||</span> <span style="color:#a6e22e">old</span><span style="color:#f92672">&gt;&gt;</span><span style="color:#a6e22e">mutexWaiterShift</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">1</span> {
					<span style="color:#a6e22e">delta</span> <span style="color:#f92672">-=</span> <span style="color:#a6e22e">mutexStarving</span>
				}
				<span style="color:#a6e22e">atomic</span>.<span style="color:#a6e22e">AddInt32</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>, <span style="color:#a6e22e">delta</span>)
				<span style="color:#66d9ef">break</span>
			}
			<span style="color:#a6e22e">awoke</span> = <span style="color:#66d9ef">true</span>
			<span style="color:#a6e22e">iter</span> = <span style="color:#ae81ff">0</span>
		} <span style="color:#66d9ef">else</span> {
			<span style="color:#a6e22e">old</span> = <span style="color:#a6e22e">m</span>.<span style="color:#a6e22e">state</span>
		}
	}

	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">race</span>.<span style="color:#a6e22e">Enabled</span> {
		<span style="color:#a6e22e">race</span>.<span style="color:#a6e22e">Acquire</span>(<span style="color:#a6e22e">unsafe</span>.<span style="color:#a6e22e">Pointer</span>(<span style="color:#a6e22e">m</span>))
	}
}</code></pre></div></p>
<h2 id="总结">总结</h2>
<p>我们从原子操作到Mutex，经历了四个层次的包装，每层包装面向不同的目的去解决不同的问题。在做发动机的时候，我们考虑的是动力、性能、省油，但拿它做飞机引擎还是做汽车引擎就要做不同的包装。我们平时做设计的时候往往也需要层进式的考虑问题，不要总是想着一步到位。</p>
</article>
 
      

      <footer class="book-footer">
        
  <div class="flex justify-between">



  <div>
    
    <a class="flex align-center" href="https://github.com/hjlarry/hjlarry.github.io/commit/f8150d4d79b0bd5434db9dad6eea68562baf2245" title='最后修改者 hjlarry | April 21, 2020' target="_blank" rel="noopener">
      <img src="/svg/calendar.svg" class="book-icon" alt="Calendar" />
      <span>April 21, 2020</span>
    </a>
  </div>



  <div>
    <a class="flex align-center" href="https://github.com/hjlarry/hjlarry.github.io/edit/master/content/docs/go/lock/_index.md" target="_blank" rel="noopener">
      <img src="/svg/edit.svg" class="book-icon" alt="Edit" />
      <span>编辑本页</span>
    </a>
  </div>

</div>

 
        <br>
<div style="text-align: center;font-size:xx-small;">
    Powered by <a href="https://gohugo.io" target="_blank">Hugo</a> | Theme by <a
        href="https://github.com/alex-shpak/hugo-book" target="_blank">hugo-book</a>
</div>
      </footer>

      
  
 

      <label for="menu-control" class="hidden book-menu-overlay"></label>
    </div>

    
    <aside class="book-toc">
      
  <nav id="TableOfContents">
  <ul>
    <li><a href="#原子操作">原子操作</a></li>
    <li><a href="#无锁结构">无锁结构</a></li>
    <li><a href="#缓存一致性">缓存一致性</a></li>
    <li><a href="#go语言中的锁">Go语言中的锁</a>
      <ul>
        <li><a href="#futex">Futex</a></li>
        <li><a href="#sema">Sema</a></li>
        <li><a href="#mutex">Mutex</a></li>
      </ul>
    </li>
    <li><a href="#总结">总结</a></li>
  </ul>
</nav>

 
    </aside>
    
  </main>

  
</body>

</html>












